#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'


from PySide import QtGui, QtCore

class InfoPage(QtGui.QDialog):
    def __init__(self, parent=None):
        super(InfoPage, self).__init__(parent=parent)
        # info page
        main_layout = QtGui.QVBoxLayout()
        info_logo_frame = QtGui.QFrame(self)
        info_logo_frame.setStyleSheet("""
            QFrame{border:1px solid rgb(20,20,20);background:rgba(20,20,20,100);border-top:none;}
            QLabel{border:none;background:rgba(0,0,0,0);}
        """)
        info_logo_frame.setMinimumHeight(23)
        info_logo_frame_layout = QtGui.QHBoxLayout()
        info_logo_frame.setLayout(info_logo_frame_layout)
        self.info_type_label = QtGui.QLabel(u'当前错误日志：None')
        info_logo_frame_layout.addWidget(self.info_type_label)
        info_logo_frame_layout.setContentsMargins(4,0,0,0)
        info_logo_frame_layout.addItem(QtGui.QSpacerItem(0,0, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding))
        self.info_edit = QtGui.QPlainTextEdit()
        self.info_edit.setStyleSheet("border:1px solid rgb(20,20,20);border-top:none;")
        self.info_edit.setReadOnly(1)
        main_layout.addWidget(info_logo_frame)
        main_layout.addWidget(self.info_edit)
        main_layout.setContentsMargins(0,0,0,0)
        main_layout.setStretch(1, 10)
        main_layout.setSpacing(0)
        self.setLayout(main_layout)

    def update_info(self, data={}, error=False):
        self.info_edit.clear()
        if error:
            self.info_type_label.setText(u'当前错误日志：None')
            self.info_edit.appendPlainText(u'请指定任务路径和类型！')
            return
        errorData = data.get('error', None)
        if not errorData:
            self.info_type_label.setText(u'当前错误日志：%s / %s' % (data['type_data']['type'], data['type_data']['sub']))
            self.info_edit.appendPlainText(u'暂时没有错误信息！！')
            return
        key_list = errorData.keys()
        key_list.sort()
        self.info_type_label.setText(u'当前错误日志：%s / %s  (%s)' % (data['type_data']['type'], data['type_data']['sub'], len(key_list)))
        for index, key in enumerate(key_list):
            data = errorData[key]
            self.info_edit.appendHtml(u"""
<head><meta charset=\"UTF-8\"></head>
<font color=red> {num} > {itemName}</font>
<pre>
     user: {user}
     time: {time}
     info: {info}
</pre>
""".format(num='{:03d}'.format(index+1), itemName=key, user=data['user'], time=data['time'], info=data['info']))

            self.info_edit.moveCursor(QtGui.QTextCursor.End)

if __name__ == "__main__":
    app = QtGui.QApplication([])
    bmui = InfoPage()
    bmui.show()
    app.exec_()
        